from pydantic import BaseModel, field_validator
from typing import Optional, List, Union
from datetime import datetime
import re

class JudgeBase(BaseModel):
    name: str

class JudgeCreate(JudgeBase):
    pass

class Judge(JudgeBase):
    id: int
    created_at: Optional[Union[datetime, str]] = None
    
    @field_validator('created_at', mode='before')
    @classmethod
    def parse_datetime(cls, v):
        if v is None:
            return datetime.now()
        if isinstance(v, str):
            # 尝试解析SQLite的datetime格式
            try:
                return datetime.fromisoformat(v.replace('Z', '+00:00'))
            except ValueError:
                # 如果解析失败，返回当前时间
                return datetime.now()
        return v
    
    class Config:
        from_attributes = True

class ContestantBase(BaseModel):
    number: str
    name: str

class ContestantCreate(ContestantBase):
    pass

class Contestant(ContestantBase):
    id: int
    is_active: bool
    created_at: Optional[Union[datetime, str]] = None
    
    @field_validator('created_at', mode='before')
    @classmethod
    def parse_datetime(cls, v):
        if v is None:
            return datetime.now()
        if isinstance(v, str):
            # 尝试解析SQLite的datetime格式
            try:
                return datetime.fromisoformat(v.replace('Z', '+00:00'))
            except ValueError:
                # 如果解析失败，返回当前时间
                return datetime.now()
        return v
    
    class Config:
        from_attributes = True

class ScoreBase(BaseModel):
    judge_id: int
    contestant_id: int
    score: float

class ScoreCreate(ScoreBase):
    pass

class Score(ScoreBase):
    id: int
    created_at: Optional[Union[datetime, str]] = None
    
    @field_validator('created_at', mode='before')
    @classmethod
    def parse_datetime(cls, v):
        if v is None:
            return datetime.now()
        if isinstance(v, str):
            # 尝试解析SQLite的datetime格式
            try:
                return datetime.fromisoformat(v.replace('Z', '+00:00'))
            except ValueError:
                # 如果解析失败，返回当前时间
                return datetime.now()
        return v
    
    class Config:
        from_attributes = True

class ScoreResponse(BaseModel):
    id: int
    judge_name: str
    contestant_name: str
    score: float
    created_at: Optional[Union[datetime, str]] = None
    
    @field_validator('created_at', mode='before')
    @classmethod
    def parse_datetime(cls, v):
        if v is None:
            return datetime.now()
        if isinstance(v, str):
            # 尝试解析SQLite的datetime格式
            try:
                return datetime.fromisoformat(v.replace('Z', '+00:00'))
            except ValueError:
                # 如果解析失败，返回当前时间
                return datetime.now()
        return v
    
    class Config:
        from_attributes = True

class ContestantResponse(BaseModel):
    id: int
    number: str
    name: str
    average_score: Optional[float] = None
    rank: Optional[int] = None
    position: Optional[int] = None
    total_scores: int = 0
    
    class Config:
        from_attributes = True 